#!/bin/sh

LOG="logger -t sysctl"

# include kernel config
. /etc/scripts/config.sh

start() {
    $LOG "Tune kernel with sysctl."
    sysctl -w -p -n -e /etc/sysctl.conf > /dev/null 2>&1

    # set reserved memory barrier
    if [ -f /tmp/is_32ram_dev ]; then
	MIN_FREE=1920
    else
	MIN_FREE=1024
    fi
    sysctl -w vm.min_free_kbytes="$MIN_FREE"

    # set domainname
    if [ "$dhcpDomain" != "" ]; then
	krn_domain="$dhcpDomain"
    else
	krn_domain="Wive-NG-RTNL"
    fi
    sysctl -w kernel.domainname="$krn_domain"

###################### SECTION FOR SAMBA AND TRANSMISION KERNEL TUNE ###################
    eval `nvram_buf_get 2860 SmbEnabled TransmissionEnabled xupnpd dhcpDomain`
    if [ "$SmbEnabled" = "1" ] || [ "$TransmissionEnabled" = "1" ] || [ "$xupnpd" = "1" ]; then
	# banish core dump files to /dev/null
	ulimit -c 0
	# increase file descriptors
	ulimit -n 32768
	# virtual memory
	ulimit -v unlimited
	# max memory size
	ulimit -m unlimited
	# always overommit
	sysctl -w vm.overcommit_ratio=100
	sysctl -w vm.overcommit_memory=2
    else
	# heruistic overcommit
	sysctl -w vm.overcommit_memory=0
    fi

#################################### SET PARANOID PARAMS###################################
    echo 1    > /proc/sys/kernel/panic
    echo 1    > /proc/sys/kernel/panic_on_oops
    echo 1    > /proc/sys/vm/panic_on_oom
}

stop() {
    :
}

case "$1" in
	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	restart)
	    stop
	    start
	    ;;

	*)
	    echo $"Usage: $0 {start|stop|restart}"
	    exit 1
esac
